package com.tac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tac.common.Exception.CosTomException;
import com.tac.common.result.Result;
import com.tac.dao.IntegralDao;
import com.tac.pojo.Integral;
import com.tac.pojo.User;
import com.tac.pojo.vo.IntegralVo;
import com.tac.service.IntegralService;
import com.tac.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntegralServiceImpl extends ServiceImpl<IntegralDao, Integral> implements IntegralService {

    @Autowired
    private UserService userService;

    /**
     * 管理员查看所有积分明细
     * @return
     */
    public Result<Page<Integral>> selectAll(Long page,Long pagesize){
        LambdaQueryWrapper<Integral> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Integral::getCreateTime);
        Page<Integral> pageinfo = new Page<>(page, pagesize);
    //    pageinfo.get
     //   List<Integral> list = this.list(queryWrapper);
        this.page(pageinfo,queryWrapper);
        List<Integral> list = pageinfo.getRecords();
        for(Integral integral:list){
            Long userId = integral.getUserId();
            User user = userService.getById(userId);
            integral.setUserPhone(user.getPhone());
        }
        Page<Integral> page1 = new Page<>();
        BeanUtils.copyProperties(pageinfo,page1,"records");
        page1.setRecords(list);
        return Result.success(page1,"success");
    }

    /**
     * 管理员修改积分记录
     * @param integral
     * @return
     */
    public Result updateIntegral(Integral integral){
        Integral integral1 = new Integral();
        integral1.setId(integral.getId());
        integral1.setEvent(integral.getEvent());
        integral1.setChange(integral.getChange());
        integral1.setChangeIdentifier(integral.getChangeIdentifier());
        boolean b = this.updateById(integral1);
        if(!b){
            throw  new CosTomException("修改异常");
        }
        return Result.success(null,"succsss");
    }

    /**
     * 管理员新增积分记录
     * @param integral
     * @return
     */
    public Result insertIntegral(Integral integral){
        Integral integral1 = new Integral();
        integral1.setEvent(integral.getEvent());
        integral1.setChange(integral.getChange());
        integral1.setChangeIdentifier(integral.getChangeIdentifier());
        integral1.setUserId(integral.getUserId());
        boolean save = this.save(integral1);
        if(!save){
            throw  new CosTomException("添加异常");
        }
        return Result.success(null,"succsss",201);
    }

    /**
     * 管理员批量删除
     * @param longs
     * @return
     */
    public Result deleteIntegral(List<Long> longs){
        boolean b = this.removeByIds(longs);
        if(!b){
            throw  new CosTomException("删除异常");
        }
        return Result.success(null,"succsss",204);
    }
    /**
     * 用户查看自己的积分明细(前十条)
     * @param uid
     * @return
     */
    public Result<IntegralVo> selectByUserTen(Long uid){
        LambdaQueryWrapper<Integral> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Integral::getUserId,uid);
        queryWrapper.orderByDesc(Integral::getCreateTime);
        List<Integral> list = this.list(queryWrapper);
        Long now=0L;
        Long cumulative=0L;
        Long consumption=0L;
        for(Integral integral:list){
            if(integral.getChangeIdentifier()==1){
                //加积分的情况
                cumulative=cumulative+integral.getChange();
            }else {
                consumption=consumption+integral.getChange();
            }
        }
        now=cumulative-consumption;

        Page<Integral> pageinfo = new Page<>(1,10);
        LambdaQueryWrapper<Integral> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Integral::getCreateTime);
        this.page(pageinfo, wrapper);

        IntegralVo integralVo = new IntegralVo();
        integralVo.setIntegralPage(pageinfo);
        integralVo.setConsumption(consumption);
        integralVo.setCumulative(cumulative);
        integralVo.setNow(now);
        return Result.success(integralVo,"succsss");
    }

    /**
     * 用户查看自己的积分明细所有
     * @param uid
     * @return
     */
    public Result<IntegralVo> selectByUser(Long uid,Long page,Long pagesize){
        LambdaQueryWrapper<Integral> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Integral::getUserId,uid);
        queryWrapper.orderByDesc(Integral::getCreateTime);
        List<Integral> list = this.list(queryWrapper);
        Long now=0L;
        Long cumulative=0L;
        Long consumption=0L;
        for(Integral integral:list){
            if(integral.getChangeIdentifier()==1){
                //加积分的情况
                cumulative=cumulative+integral.getChange();
            }else {
                consumption=consumption+integral.getChange();
            }
        }
        now=cumulative-consumption;

        Page<Integral> pageinfo = new Page<>(page,pagesize);
        LambdaQueryWrapper<Integral> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Integral::getCreateTime);
        this.page(pageinfo, wrapper);

        IntegralVo integralVo = new IntegralVo();
        integralVo.setIntegralPage(pageinfo);
        integralVo.setConsumption(consumption);
        integralVo.setCumulative(cumulative);
        integralVo.setNow(now);
        return Result.success(integralVo,"succsss");
    }
}
